<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Extending MatrixBase (and other classes)</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('TopicCustomizing_Plugins.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">Extending <a class="el" href="classEigen_1_1MatrixBase.html" title="Base class for all dense matrices, vectors, and expressions.">MatrixBase</a> (and other classes) </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>In this section we will see how to add custom methods to <a class="el" href="classEigen_1_1MatrixBase.html" title="Base class for all dense matrices, vectors, and expressions.">MatrixBase</a>. Since all expressions and matrix types inherit <a class="el" href="classEigen_1_1MatrixBase.html" title="Base class for all dense matrices, vectors, and expressions.">MatrixBase</a>, adding a method to <a class="el" href="classEigen_1_1MatrixBase.html" title="Base class for all dense matrices, vectors, and expressions.">MatrixBase</a> make it immediately available to all expressions ! A typical use case is, for instance, to make <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> compatible with another API.</p>
<p>You certainly know that in C++ it is not possible to add methods to an existing class. So how that's possible ? Here the trick is to include in the declaration of <a class="el" href="classEigen_1_1MatrixBase.html" title="Base class for all dense matrices, vectors, and expressions.">MatrixBase</a> a file defined by the preprocessor token <code>EIGEN_MATRIXBASE_PLUGIN:</code> </p><div class="fragment"><div class="line"><span class="keyword">class </span>MatrixBase {</div>
<div class="line">  <span class="comment">// ...</span></div>
<div class="line"><span class="preprocessor">  #ifdef EIGEN_MATRIXBASE_PLUGIN</span></div>
<div class="line"><span class="preprocessor">  #include EIGEN_MATRIXBASE_PLUGIN</span></div>
<div class="line"><span class="preprocessor">  #endif</span></div>
<div class="line"><span class="preprocessor">};</span></div>
</div><!-- fragment --><p> Therefore to extend <a class="el" href="classEigen_1_1MatrixBase.html" title="Base class for all dense matrices, vectors, and expressions.">MatrixBase</a> with your own methods you just have to create a file with your method declaration and define EIGEN_MATRIXBASE_PLUGIN before you include any <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>'s header file.</p>
<p>You can extend many of the other classes used in <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> by defining similarly named preprocessor symbols. For instance, define <code>EIGEN_ARRAYBASE_PLUGIN</code> if you want to extend the <a class="el" href="classEigen_1_1ArrayBase.html" title="Base class for all 1D and 2D array, and related expressions.">ArrayBase</a> class. A full list of classes that can be extended in this way and the corresponding preprocessor symbols can be found on our page <a class="el" href="TopicPreprocessorDirectives.html">Preprocessor directives</a>.</p>
<p>Here is an example of an extension file for adding methods to <a class="el" href="classEigen_1_1MatrixBase.html" title="Base class for all dense matrices, vectors, and expressions.">MatrixBase</a>: <br  />
<b>MatrixBaseAddons.h</b> </p><div class="fragment"><div class="line"><span class="keyword">inline</span> Scalar at(uint i, uint j)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> this-&gt;operator()(i,j); }</div>
<div class="line"><span class="keyword">inline</span> Scalar&amp; at(uint i, uint j) { <span class="keywordflow">return</span> this-&gt;operator()(i,j); }</div>
<div class="line"><span class="keyword">inline</span> Scalar at(uint i)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> this-&gt;operator[](i); }</div>
<div class="line"><span class="keyword">inline</span> Scalar&amp; at(uint i) { <span class="keywordflow">return</span> this-&gt;operator[](i); }</div>
<div class="line"> </div>
<div class="line"><span class="keyword">inline</span> RealScalar squaredLength()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> squaredNorm(); }</div>
<div class="line"><span class="keyword">inline</span> RealScalar length()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> norm(); }</div>
<div class="line"><span class="keyword">inline</span> RealScalar invLength(<span class="keywordtype">void</span>)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> fast_inv_sqrt(squaredNorm()); }</div>
<div class="line"> </div>
<div class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> OtherDerived&gt;</div>
<div class="line"><span class="keyword">inline</span> Scalar squaredDistanceTo(<span class="keyword">const</span> MatrixBase&lt;OtherDerived&gt;&amp; other)<span class="keyword"> const</span></div>
<div class="line"><span class="keyword"></span>{ <span class="keywordflow">return</span> (derived() - other.derived()).squaredNorm(); }</div>
<div class="line"> </div>
<div class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> OtherDerived&gt;</div>
<div class="line"><span class="keyword">inline</span> RealScalar distanceTo(<span class="keyword">const</span> MatrixBase&lt;OtherDerived&gt;&amp; other)<span class="keyword"> const</span></div>
<div class="line"><span class="keyword"></span>{ <span class="keywordflow">return</span> internal::sqrt(derived().squaredDistanceTo(other)); }</div>
<div class="line"> </div>
<div class="line"><span class="keyword">inline</span> <span class="keywordtype">void</span> scaleTo(RealScalar l) { RealScalar vl = norm(); <span class="keywordflow">if</span> (vl&gt;1e-9) derived() *= (l/vl); }</div>
<div class="line"> </div>
<div class="line"><span class="keyword">inline</span> Transpose&lt;Derived&gt; transposed() {<span class="keywordflow">return</span> this-&gt;transpose();}</div>
<div class="line"><span class="keyword">inline</span> <span class="keyword">const</span> Transpose&lt;Derived&gt; transposed()<span class="keyword"> const </span>{<span class="keywordflow">return</span> this-&gt;transpose();}</div>
<div class="line"> </div>
<div class="line"><span class="keyword">inline</span> uint minComponentId(<span class="keywordtype">void</span>)<span class="keyword"> const  </span>{ <span class="keywordtype">int</span> i; this-&gt;minCoeff(&amp;i); <span class="keywordflow">return</span> i; }</div>
<div class="line"><span class="keyword">inline</span> uint maxComponentId(<span class="keywordtype">void</span>)<span class="keyword"> const  </span>{ <span class="keywordtype">int</span> i; this-&gt;maxCoeff(&amp;i); <span class="keywordflow">return</span> i; }</div>
<div class="line"> </div>
<div class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> OtherDerived&gt;</div>
<div class="line"><span class="keywordtype">void</span> makeFloor(<span class="keyword">const</span> MatrixBase&lt;OtherDerived&gt;&amp; other) { derived() = derived().cwiseMin(other.derived()); }</div>
<div class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> OtherDerived&gt;</div>
<div class="line"><span class="keywordtype">void</span> makeCeil(<span class="keyword">const</span> MatrixBase&lt;OtherDerived&gt;&amp; other) { derived() = derived().cwiseMax(other.derived()); }</div>
<div class="line"> </div>
<div class="line"><span class="keyword">const</span> CwiseBinaryOp&lt;internal::scalar_sum_op&lt;Scalar&gt;, <span class="keyword">const</span> Derived, <span class="keyword">const</span> ConstantReturnType&gt;</div>
<div class="line">operator+(<span class="keyword">const</span> Scalar&amp; scalar)<span class="keyword"> const</span></div>
<div class="line"><span class="keyword"></span>{ <span class="keywordflow">return</span> CwiseBinaryOp&lt;internal::scalar_sum_op&lt;Scalar&gt;, <span class="keyword">const</span> Derived, <span class="keyword">const</span> ConstantReturnType&gt;(derived(), Constant(rows(),cols(),scalar)); }</div>
<div class="line"> </div>
<div class="line"><span class="keyword">friend</span> <span class="keyword">const</span> CwiseBinaryOp&lt;internal::scalar_sum_op&lt;Scalar&gt;, <span class="keyword">const</span> ConstantReturnType, Derived&gt;</div>
<div class="line">operator+(<span class="keyword">const</span> Scalar&amp; scalar, <span class="keyword">const</span> MatrixBase&lt;Derived&gt;&amp; mat)</div>
<div class="line">{ <span class="keywordflow">return</span> CwiseBinaryOp&lt;internal::scalar_sum_op&lt;Scalar&gt;, <span class="keyword">const</span> ConstantReturnType, Derived&gt;(Constant(rows(),cols(),scalar), mat.derived()); }</div>
</div><!-- fragment --><p>Then one can the following declaration in the config.h or whatever prerequisites header file of his project: </p><div class="fragment"><div class="line"><span class="preprocessor">#define EIGEN_MATRIXBASE_PLUGIN &quot;MatrixBaseAddons.h&quot;</span></div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
